<?php
    header("Content-type:text/html;charset=utf-8");
    $name = $_POST["name"];
    $password = $_POST["password"];
    if($name === "" || $password === ""){
        die("用户名或密码不能为空！");
    }

    $db_username = "root";
    $db_password = "root";
    $db_host = "localhost:3306";
    $con = mysql_connect($db_host, $db_username, $db_password);
    if(!$con){
        $json_arr = array("statu" => "error", "stateCode" => "2", "errorType" => "mySQL error".mysql_error());
        echo json_encode($json_arr);
    }

    mysql_select_db("project", $con);

    $select_SQL = "SELECT username FROM user";
    $select_source = mysql_query($select_SQL, $con);
    if($select_source){
        while($row = mysql_fetch_array($select_source)){
            if($row["username"] === $name){
                $json_arr = array("statu" => "error", "stateCode" => 4, "errorType" => "repeat of username");
                die(json_encode($json_arr));
            }
        }
    }

    $insert_SQL = "INSERT INTO user(
        `username`,
        `password`
    )
        VALUES
    (
        '$name',
        '$password'
    )";

    $res = mysql_query($insert_SQL, $con);
    if(!$res){
        $json_arr = array("statu" => "error", "stateCode" => "3", "errorType" => "insert error".mysql_error());
        die(json_encode($json_arr));
    }
    $json_arr = array("statu" => "success", "stateCode" => "1", "password" => $password);
    echo json_encode($json_arr);
?>